#pragma once
#include <string>
#include <functional>
#include "Protocol.hpp"


namespace wxd
{
    using parse_task_t = std::function<Response(const Request&)>;

    class Parse
    {
        public:
        Parse(){};
        Parse(parse_task_t func)
        :_func(func)
        {}
        //len\r\n{json}\r\n  len\r\n{json}\r\n
        //处理成结构化的Request
        //传给Calculator
        //得到结构化的Response
        //处理成序列化的Response
        //添加报头，返回去
        std::string ExecuteParse(std::string& package_request)
        {
            std::string package_response;
            std::string content_request;
            while(Decode(package_request,content_request))
            {
                //decode成功 content为载荷
                //拿着载荷，反序列化,得到结构化的Request
                Request request;
                request.Deserialize(content_request);
                //调用方法得到结构化的response
                Response new_response = _func(request);
                std::string content_response;
                //序列化response
                new_response.Serialize(content_response);
                //encode
                Encode(content_response);
                package_response += content_response;
            }
            //只要成功一次就返回序列化+encode后的response
            //否则返回空串，表示解析失败
            return package_response;
        }

        private:
            parse_task_t _func;
    };
}